package cn.tedu.mintmall.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Data
@ApiModel(value="自定义认证框架数据封装")
@Component
public class GetInfoUtils {
    /**
     * 用户id 可以是admin用户也可以是普通user用户id
     */
    @ApiModelProperty(value="用户id")
    private Long id;
    @ApiModelProperty(value="用户名")
    private String username;
    @ApiModelProperty(value="用户类型")
    private String userType;
    @ApiModelProperty(value="用户权限列表")
    private List<String> authorities;

    public GetInfoUtils getUserInfo() throws SecurityException {
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder
                        .getContext().getAuthentication();
        if (authenticationToken != null) {
            GetInfoUtils info = (GetInfoUtils) authenticationToken.getCredentials();
            return info;
        } else {
            throw new SecurityException("没有登录账户");
        }
    }
}
